%{
	#include<stdio.h>
	#include<ctype.h>
	#include<stdlib.h>
	#include<string.h>
	#ifndef YYSTYPE
	#define YYSTYPE char*
	#endif
	char idStr[50];
	char numStr[50];
	int yylex();
	extern int yyparse();
	FILE* yyin;
	void yyerror(char const*); 
%}
%token NUMBER
%token ID
%left '+' '-'
%left '*' '/'
%right UMINUS

%%
lines	:	lines expr ';'		{printf("%s\n",$2);}
		|	lines ';'
		|
		;
expr	:	expr'+'expr		{$$=(char*)malloc(50*sizeof(char));strcpy($$,$1);strcat($$,$3);strcat($$,"+");}
		|	expr'-'expr	{$$=(char*)malloc(50*sizeof(char));strcpy($$,$1);strcat($$,$3);strcat($$,"-");}
		|	expr'*'expr	{$$=(char*)malloc(50*sizeof(char));strcpy($$,$1);strcat($$,$3);strcat($$,"*");}
		|	expr'/'expr	{$$=(char*)malloc(50*sizeof(char));strcpy($$,$1);strcat($$,$3);strcat($$,"/");}
		|	'('expr')'	{$$=(char*)malloc(50*sizeof(char));strcpy($$,$2);}
		|	'-'expr %prec UMINUS {$$=(char*)malloc(50*sizeof(char));strcpy($$,"(-");strcat($$,$2);strcat($$,")");}
		|	NUMBER		{$$=(char*)malloc(50*sizeof(char));strcpy($$,$1);strcat($$," ");}
		|	ID		{$$=(char*)malloc(50*sizeof(char));strcpy($$,$1);strcat($$," ");}
		;

%%
int main(){
	yyin=stdin;
	do{
		yyparse();
	}while(!feof(yyin));
	return 0;
}
int yylex(){
	int t;
	while(1)
	{
		t=getchar();
		if(t==' '||t=='\t'||t=='\n'){}
		else if(isdigit(t))
		{
			int ti=0;
			while (( t >= '0' && t <='9')) 
			{
				numStr[ ti]=t ;
				t = getchar ();
				ti++;
			}
			numStr[ti]='\0';
			yylval=numStr;
			ungetc(t,stdin);
			return NUMBER;
		}
		else if(( t >= 'a' && t <='z')||(t>='A'&&t<='Z')||(t=='_')||(t>='0'&&t<='9'))
		{
			int ti=0;
			while(( t >= 'a' && t <='z')||(t>='A'&&t<='Z')||(t=='_')||(t>='0'&&t<='9'))
			{
				idStr[ti]=t ;
				t = getchar ();
				ti++;
			}
			idStr[ti]='\0';
			yylval=idStr;
			ungetc(t,stdin);
			return ID;
		}
		else
		{
			return t;
		}	
	}
}
void yyerror(char const*s){
	fprintf(stderr,"%s\n",s);
}
